﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows.Input;
using CLEX.AdminUI.View;

namespace CLEX.AdminUI.ViewModel
{
    internal sealed class UserListVM : ViewModelBase
    {
        private readonly Facade facade;
        private readonly ObservableCollection<UserReadVM> users = new ObservableCollection<UserReadVM>();
        private UserReadVM selectedUser;
        private RelayCommand newUser;
        private RelayCommand editUser;
        private RelayCommand deleteUser;

        public IEnumerable<UserReadVM> Users
        {
            get { return users; }
        }

        public UserReadVM SelectedUser
        {
            get { return selectedUser; }
            set
            {
                if (selectedUser != value)
                {
                    selectedUser = value;
                    FirePropertyChanged("SelectedUser");
                }
            }
        }

        public ICommand NewUser
        {
            get
            {
                if (newUser == null)
                {
                    newUser = new RelayCommand(DoNewUser);
                }
                return newUser;
            }
        }

        public ICommand EditUser
        {
            get
            {
                if (editUser == null)
                {
                    editUser = new RelayCommand(DoEditUser, CanEditUser);
                }
                return editUser;
            }
        }

        public ICommand DeleteUser
        {
            get
            {
                if (deleteUser == null)
                {
                    deleteUser = new RelayCommand(DoDeleteUser, CanDeleteUser);
                }
                return deleteUser;
            }
        }

        public UserListVM(Facade facade)
        {
            this.facade = facade;
            Refresh();
        }

        private void Refresh()
        {
            users.Clear();
            users.AddRange(facade.GetAllUsers().Select(p => new UserReadVM(p)));
        }

        private void DoNewUser()
        {
            UserCreateView vw = new UserCreateView(new UserCreateVM(facade));
            vw.ShowDialog();
            Refresh();
        }

        private void DoEditUser()
        {
            UserEditView vw = new UserEditView(new UserEditVM(facade, selectedUser.GetData()));
            vw.ShowDialog();
            Refresh();
        }

        private bool CanEditUser()
        {
            return SelectedUser != null;
        }

        private void DoDeleteUser()
        {
            if (facade.ConfirmDelete("user " + SelectedUser.Username))
            {
                facade.DeleteUser(SelectedUser.GetData());
                Refresh();
            }
        }

        private bool CanDeleteUser()
        {
            return SelectedUser != null;
        }
    }
}
